package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;

import net.miginfocom.swing.MigLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSlider;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import classes.ConnectionDB;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

public class ViewTopics extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JFrame accessFrame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ViewTopics frame = new ViewTopics(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 */
	public ViewTopics(Properties langfile) throws SQLException {
		accessFrame = this;
		this.setUndecorated(true);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.textInactiveText);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel(langfile.getProperty("usr.menu.view.topics.title"));
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		final Connection connection = new ConnectionDB().createConnection();
		
		java.sql.Statement statemnt = connection.createStatement();
		
		ResultSet result = statemnt.executeQuery("SELECT DISTINCT topic FROM TOPSUB");
		
		ArrayList<String> topics = new ArrayList<String>();
		
		while (result.next()) {
			topics.add(result.getString("topic")); 	
		}
		
		final JComboBox topicsList = new JComboBox();
		topicsList.setMaximumRowCount(15);

		for(String str: topics)
		{
			topicsList.addItem(str);
		}
		Vector<String> v = new Vector<String>();
		v.addElement("Subtopics");
		final DefaultTableModel t = new DefaultTableModel(null,v);
		table = new JTable(t);
		table.setVisible(false);
		
		topicsList.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				java.sql.PreparedStatement stat = null;
				try {
					stat = connection.prepareStatement("SELECT subtopic FROM TOPSUB WHERE topic = ?");
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
				try {
					String item = (String)topicsList.getSelectedItem();
					stat.setString(1,item);
					ResultSet res = stat.executeQuery();

					while(res.next()){
						Vector<String> data = new Vector<String>();
						data.addElement(res.getString("subtopic"));
						t.addRow(data);					
					}
					table.setVisible(true);
					
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		
		JLabel lblSelectATopic = new JLabel(langfile.getProperty("usr.menu.file.addTopic.chooseTopic"));
		lblSelectATopic.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblSelectATopic.setHorizontalAlignment(SwingConstants.CENTER);
		
		JButton btnMainMenu = new JButton(langfile.getProperty("usr.menu.backToMainMenu"));
		btnMainMenu.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				accessFrame.setVisible(false);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(193)
					.addComponent(lblSelectATopic, GroupLayout.PREFERRED_SIZE, 204, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(topicsList, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
					.addGap(55)
					.addComponent(table, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(btnMainMenu)
					.addGap(21)
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
					.addGap(108))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(btnMainMenu))
					.addGap(52)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelectATopic)
						.addComponent(topicsList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(table, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
					.addGap(151))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
